package com.fenghongzhang.day009;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MySqlHelp extends SQLiteOpenHelper {


    /**
     *
     * @param context 上下文
     * @param name  数据库的名字  加.db
     * @param factory null 用系统自带的
     * @param version  2
     */
    public MySqlHelp(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /**
     * 创建数据库
     * 只执行一次
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create TABLE student (id INTEGER PRIMARY key autoincrement , name VARCHAR(20),age integer)");
//        db.execSQL("");
        //1,原生sql
        String sql = "insert INTO student VALUES (null,?,?)";

        //开启
        db.beginTransaction();

        db.execSQL(sql,new Object[]{"小明",18});
        db.execSQL(sql,new Object[]{"小明",18});
        db.execSQL(sql,new Object[]{"小明",18});

        db.setTransactionSuccessful();

        db.endTransaction();
    }


    /**
     * 更新数据库
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        db.execSQL("alter table");
    }
}
